package core.icq;
import core.IncomeMessage;
import core.IMessageSender;
import java.util.UUID;
import ru.caffeineim.protocols.icq.core.OscarConnection;
import ru.caffeineim.protocols.icq.integration.OscarInterface;
import ru.caffeineim.protocols.icq.integration.events.IncomingMessageEvent;
import tools.Logger;

/**
 *
 * @author dcrm
 */
public class IncomeMsg implements IMessageSender,Runnable {

    private OscarConnection OSCAR_CONNECTION;
    private IncomingMessageEvent IncomingEvent;
    private String IncomingMessage;
    private int MSG_MAX_PART; // Max Size part MSG;
    private CoreICQ CORE;
    
    private Thread thread;
    private final String THREAD_NAME ="MSG["+UUID.randomUUID()+"]";

    public IncomeMsg(IncomingMessageEvent e, CoreICQ core) {
        this.CORE=core;
        this.OSCAR_CONNECTION=core.connection;
        this.IncomingEvent=e;
        this.IncomingMessage=e.getMessage();
        this.MSG_MAX_PART=2000;
        thread = new Thread(this);
        thread.setName(THREAD_NAME);
        thread.start();
    }

    // Send Set State Boolean to Admin
    public void sendBAdmReport(boolean b,String s) {
        try { OscarInterface.sendBasicMessage(OSCAR_CONNECTION, CORE.MASTER_UIN, s + " - state: " + ((b) ? "ON" : "OFF"));
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void sendAdMsg(String s) {
        try { OscarInterface.sendBasicMessage(OSCAR_CONNECTION, CORE.MASTER_UIN, s);
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void sendAdEMsg(String s) {
        try {
            if (s.length()>MSG_MAX_PART)
            {
                String tmp="";
                int part_0=0,part=MSG_MAX_PART,size_all=s.length(),prt=0,all=(int)(size_all/part);
                while(size_all>0)
                {
                    if(size_all>part) {tmp="Part #["+prt+++"/"+all+"]:\n"+s.substring(part_0, part_0+=part);}
                    else {tmp=s.substring(part_0);}
                    OscarInterface.sendExtendedMessage(OSCAR_CONNECTION, CORE.MASTER_UIN, new String(tmp.getBytes(), "UTF-8"));
                    size_all-=part;
                    Thread.sleep(5000);
                }
            } else OscarInterface.sendExtendedMessage(OSCAR_CONNECTION, CORE.MASTER_UIN, new String(s.getBytes(), "UTF-8"));
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}

    }

    public void sendMessage(String to, String msg) {
        try { OscarInterface.sendBasicMessage(OSCAR_CONNECTION, to, msg);
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void sendExtendedMessage(String to, String msg) {
        try { OscarInterface.sendExtendedMessage(OSCAR_CONNECTION, to, msg);
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void reConnect() {
        try { CORE.reConnect();
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void disconnect(boolean bl) {
        try { CORE.disconnect(bl);
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }
    
    public void run() {
        try { new IncomeMessage(IncomingMessage, IncomingEvent.getSenderID().toString(), CORE.MASTER_UIN.toString(), MSG_MAX_PART, THREAD_NAME, this);  }
        catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public String getInfo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
